#include <bits/stdc++.h>
#define gc getchar
#define pc putchar
#define pt printf
#define int long long
#define For(i,a,b) for(int i=a;i<=b;++i)
#define Rof(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int Mxn=1005,Mod=147744151;
int N,M,P,Q,Ans,A[Mxn][Mxn],F[Mxn][Mxn];
int _() {
	int x=0,f=0;char ch=gc();
	while(ch<'0'||ch>'9') f|=(ch=='-'),ch=gc();
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=gc();
	return f?-x:x;	
}
void __(int x) {
	int stk[30],tp=0;
	if(x<0) x=-x,pc('-');
	do stk[++tp]=x%10,x/=10;while(x);
	while(tp) pc(stk[tp--]^48);
}
void Task1() {
	Q=_();
	For(t,1,Q) {
		int x1=_(),y1=_(),x2=_(),y2=_();
		int Res=1;
		For(i,x1,x2) For(j,y1,y2) {
			(Res*=A[i][j])%=P;
		}
		(Ans+=Res^t)%=Mod;
	}
	__(Ans);
}
int Power(int x,int b) {
	int s=1;
	while(b) {
		if(b&1) (s*=x)%=P;
		(x*=x)%=P,b>>=1;
	}
	return s;
}
int Iv(int x) {return Power(x,P-2);}
void Task2() {
	For(i,0,N) F[i][0]=1;
	For(i,0,M) F[0][i]=1;
	For(i,1,N) For(j,1,M) {
		F[i][j]=F[i-1][j]*F[i][j-1]%P*Iv(F[i-1][j-1])%P*A[i][j]%P;
	}
	Q=_();
	For(t,1,Q) {
		int x1=_(),y1=_(),x2=_(),y2=_();
		int Res=F[x2][y2]*Iv(F[x2][y1-1])%P*Iv(F[x1-1][y2])%P*F[x1-1][y1-1]%P;
		(Ans+=t^Res)%=Mod;
	}
	__(Ans);
}
signed main() {
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	N=_(),M=_(),P=_();
	For(i,1,N) For(j,1,M) A[i][j]=_();
	if(N<=200&&M<=200&&Q<=200) Task1();
	else Task2();
	return 0;
}
